package com.hq.common.utils;


import sun.misc.BASE64Decoder;
import sun.misc.BASE64Encoder;

import javax.crypto.*;
import javax.crypto.spec.DESKeySpec;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.InvalidKeyException;
import java.security.Key;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;

/**

 * @since 2015-01-18 23:34
 */
public class EncrpytUtils {

    /**
     * DES加密算法
     */
    public final static String ALGORITHM_DES = "DES";

    private static final String DEFAULT_ENCRYPTKEY = "{SGCC}EPGISSERVER";


    /**
     * 加密字符串
     *
     * @param code       需要加密的字符串
     * @param encryptKey 对称密钥串
     * @throws java.security.NoSuchAlgorithmException
     * @throws java.io.UnsupportedEncodingException
     * @throws java.security.InvalidKeyException
     * @throws javax.crypto.NoSuchPaddingException
     * @throws javax.crypto.IllegalBlockSizeException
     * @throws javax.crypto.BadPaddingException
     * @throws InvalidKeySpecException
     */
    public static String encryptDES(String code, String encryptKey) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException {
        BASE64Encoder base64en = new BASE64Encoder();
        byte[] bytesMing = encryptDES(code.getBytes("UTF8"), encryptKey);
        return base64en.encode(bytesMing);
    }

    public static String encryptDES(String code) throws NoSuchAlgorithmException, UnsupportedEncodingException, InvalidKeyException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException {
        return encryptDES(code, DEFAULT_ENCRYPTKEY);
    }

    public static String decryptDES(String code) throws IOException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException {
        return decryptDES(code, DEFAULT_ENCRYPTKEY);
    }


    /**
     * 解密字符串
     *
     * @param code       需要解密的字符串
     * @param encryptKey 对称密钥串
     * @throws java.io.IOException
     * @throws java.security.InvalidKeyException
     * @throws java.security.NoSuchAlgorithmException
     * @throws javax.crypto.NoSuchPaddingException
     * @throws javax.crypto.IllegalBlockSizeException
     * @throws javax.crypto.BadPaddingException
     * @throws InvalidKeySpecException
     */
    public static String decryptDES(String code, String encryptKey) throws IOException, InvalidKeyException, NoSuchAlgorithmException, NoSuchPaddingException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException {
        BASE64Decoder base64De = new BASE64Decoder();
        byte[] bytesMi = base64De.decodeBuffer(code);
        byte[] bytesMing = decryptDES(bytesMi, encryptKey);
        return new String(bytesMing, "UTF8");
    }


    /**
     * 加密
     *
     * @param bytes      明文字节
     * @param encryptKey 对称密钥串
     * @throws java.security.NoSuchAlgorithmException
     * @throws javax.crypto.NoSuchPaddingException
     * @throws java.security.InvalidKeyException
     * @throws javax.crypto.IllegalBlockSizeException
     * @throws javax.crypto.BadPaddingException
     * @throws InvalidKeySpecException
     */
    public static byte[] encryptDES(byte[] bytes, String encryptKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException, UnsupportedEncodingException {
        Key key = generateKey(encryptKey);
        Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
        cipher.init(Cipher.ENCRYPT_MODE, key);
        return cipher.doFinal(bytes);
    }


    /**
     * 生成密钥
     *
     * @param encryptKey 加密密钥
     * @throws java.security.NoSuchAlgorithmException
     * @throws InvalidKeyException
     * @throws InvalidKeySpecException
     */
    public static Key generateKey(String encryptKey) throws NoSuchAlgorithmException, InvalidKeyException, InvalidKeySpecException, UnsupportedEncodingException {
        /*KeyGenerator keyGenerator = KeyGenerator.getInstance(ALGORITHM_DES);
        keyGenerator.init(new SecureRandom(encryptKey.getBytes()));
		Key key = keyGenerator.generateKey();*/

        SecretKeyFactory keyFactory = SecretKeyFactory.getInstance(ALGORITHM_DES);
        DESKeySpec keySpec = new DESKeySpec(encryptKey.getBytes("UTF-8"));
        return keyFactory.generateSecret(keySpec);
    }

    /**
     * 解密
     *
     * @param bytes      密文字节
     * @param encryptKey 对称密钥串
     * @throws java.security.NoSuchAlgorithmException
     * @throws javax.crypto.NoSuchPaddingException
     * @throws java.security.InvalidKeyException
     * @throws javax.crypto.IllegalBlockSizeException
     * @throws javax.crypto.BadPaddingException
     * @throws InvalidKeySpecException
     */
    public static byte[] decryptDES(byte[] bytes, String encryptKey) throws NoSuchAlgorithmException, NoSuchPaddingException, InvalidKeyException, IllegalBlockSizeException, BadPaddingException, InvalidKeySpecException, UnsupportedEncodingException {
        Key key = generateKey(encryptKey);
        Cipher cipher = Cipher.getInstance(ALGORITHM_DES);
        cipher.init(Cipher.DECRYPT_MODE, key);
        return cipher.doFinal(bytes);
    }


    public static void main(String[] args) throws Exception {
        String s = encryptDES("geostar", "abcdefghigklmn123456opqrstuvwxyz");
        System.out.println(s);
        String d = decryptDES(s, "abcdefghigklmn123456opqrstuvwxyz");
        System.out.println(d);
    }
}